﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using cbmobile.DAC;
using PQT.API;
using System.Data;
using cbmobile.BLC;
using System.Data.SqlClient;

namespace cbmobile.BLC
{
    public class BLC_LinhKien
    {
        private CBMobileEntities da = null;
        DBConnection dbCn = new DBConnection("PQT");
        SqlConnection cnn = null;


        public BLC_LinhKien()
        {
            da = new CBMobileEntities();
        }

        public IList<LinhKien> All_Components()
        {
            return da.LinhKiens.ToList();
        }

        public DataTable Get_LK_Top()
        {
            DataTable dt = null;
            using (cnn = dbCn.InitConnection())
            {
                cnn.Open();
                SqlDataAdapter da = null;
                DataSet ds = new DataSet();
                SqlCommand cmdUp = new SqlCommand("pr_Get_LK_Top", cnn);
                cmdUp.CommandType = CommandType.StoredProcedure;
                try
                {
                    da = new SqlDataAdapter(cmdUp);
                    dt = new DataTable();
                    da.Fill(dt);
                }
                catch { }
                finally
                {
                    if (cnn.State == ConnectionState.Open)
                        cnn.Close();
                    cnn.Dispose();
                }

            }
            return dt;
        }


        public LinhKien Get_Components_By_ID(int Ma_LinhKien)
        {
            IList<LinhKien> list = da.LinhKiens.Where(z => z.Ma_LinhKien == Ma_LinhKien).ToList();
            if (list.Count() == 0)
                return null;
            return list.First();
        }

        public IList<LinhKien> Row_LinhKien(int p_page, int p_pageSize, string p_name)
        {
            int preCount = (p_page - 1) * p_pageSize;
            IList<LinhKien> list = da.LinhKiens.Where(z => z.TenLinhKien.Contains(p_name)).OrderByDescending(z => z.TenLinhKien).Skip(preCount).Take(preCount + p_pageSize).ToList();
            return list;
        }

        public LoaiLinhKien Get_LK_By_NameLK(string TenLoai_LK)
        {
            IList<LoaiLinhKien> list = da.LoaiLinhKiens.Where(z => z.Ten_LoaiLinhKien.Contains(TenLoai_LK)).ToList();
            if (list.Count() == 0)
                return null;
            return list.First();
        }

        public IList<LinhKien> Get_LK_By_IDMaLK(int MaLK)
        {
            IList<LinhKien> list = da.LinhKiens.Where(z => z.Ma_LoaiLinhKien == MaLK).ToList();
            if (list.Count() == 0)
                return null;
            return list;
        }


        public int GetTotalRecord(string p_name)
        {
            return da.LinhKiens.Where(z => z.TenLinhKien.Contains(p_name)).Count();
        }


        public void Delete(int Ma_LinhKien)
        {
            var existing = da.LinhKiens.Where(p => p.Ma_LinhKien == Ma_LinhKien).First();
            existing.TinhTrang = (int)CommonStatus.Delete;
            da.SaveChanges();
        }


        #region create
        public void Create(int Ma_LoaiLinhKien, string TenLinhKien, int TinhTrang, string ChatLieu,
                                string MauSac, string NguyenHop,
                                string BaoHanh, decimal Gia, int SoLuotXem, string HinhAnh,
                                string MoTa, string BaiViet, DateTime NgayTao)
        {
            var objEnt = new LinhKien();
            objEnt.Ma_LoaiLinhKien = Ma_LoaiLinhKien;
            objEnt.TenLinhKien = TenLinhKien;
            objEnt.TinhTrang = TinhTrang;
            objEnt.ChatLieu = ChatLieu;
            objEnt.MauSac = MauSac;
            objEnt.NguyenHop = NguyenHop;
            objEnt.BaoHanh = BaoHanh;
            objEnt.Gia = Gia;
            objEnt.SoLuotXem = SoLuotXem;
            objEnt.HinhAnh = HinhAnh;
            objEnt.MoTa = MoTa;
            objEnt.BaiViet = BaiViet;
            objEnt.NgayTao=NgayTao;

            da.LinhKiens.AddObject(objEnt);
            da.SaveChanges();
        }
        #endregion

        #region update
        public void Update(int Ma_LinhKien, int Ma_LoaiLinhKien, string TenLinhKien, int TinhTrang, string ChatLieu,
                                string MauSac, string NguyenHop,
                                string BaoHanh, decimal Gia, int SoLuotXem, string HinhAnh,
                                string MoTa, string BaiViet, DateTime NgayTao)
        {
            var role = new LinhKien();
            var objEnt = da.LinhKiens.Where(p => p.Ma_LinhKien == Ma_LinhKien).First();

            objEnt.Ma_LoaiLinhKien = Ma_LoaiLinhKien;
            objEnt.TenLinhKien = TenLinhKien;
            objEnt.TinhTrang = TinhTrang;
            objEnt.ChatLieu = ChatLieu;
            objEnt.MauSac = MauSac;
            objEnt.NguyenHop = NguyenHop;
            objEnt.BaoHanh = BaoHanh;
            objEnt.Gia = Gia;
            objEnt.SoLuotXem = SoLuotXem;
            objEnt.HinhAnh = HinhAnh;
            objEnt.MoTa = MoTa;
            objEnt.BaiViet = BaiViet;
            objEnt.NgayTao = NgayTao;

            da.SaveChanges();
        }
        #endregion

        public void Create(PhuKien role)
        {
            da.PhuKiens.AddObject(role);
            da.SaveChanges();
        }
    }
}